/*
 * @Project Name: spring-boot
 * @File Name: UserService
 * @Package Name: com.hhly.spring.boot.cache
 * @Date: 2017/12/13 17:52
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.service.base.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.dao.mapper.base.MainIndexMapper;
import com.sm.shoplook.domain.base.MainIndex;
import com.sm.shoplook.service.base.MainIndexService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.base.BaseSearchVo;

/**
 * @author chenpy-1072
 * @desc 首页管理
 * @date 2017/12/13 17:52
 * @see
 */
@Service
public class MainIndexServiceImpl implements MainIndexService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MainIndexServiceImpl.class);
	@Autowired
	private MainIndexMapper mainIndexMapper;

	@Override
	public ApiResult<PageInfo<MainIndex>> getListPage(BaseSearchVo baseSearchVo) {
		PageHelper.startPage(baseSearchVo.getOffset(), baseSearchVo.getPageSize());
		List<MainIndex> materials = mainIndexMapper.getListPage(baseSearchVo);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, new PageInfo<>(materials));
	}

	@Override
	public ApiResult batchDelete(String[] idList) throws ApiException {
		int i = mainIndexMapper.batchDelete(idList);
		if (i < 1) {
			throw new ApiException();
		}
		return new ApiResult(ResultMessage.RESULT_SUCCESS_1);
	}

	@Override
	public ApiResult saveMainIndex(MainIndex mainIndex) throws ApiException {
		int i;
		if (mainIndex.getId() == null) {
			i = mainIndexMapper.insertSelective(mainIndex);
		} else {
			i = mainIndexMapper.updateByPrimaryKeySelective(mainIndex);
		}
		if (i < 1) {
			throw new ApiException();
		}
		return new ApiResult(ResultMessage.RESULT_SUCCESS_1);
	}

	@Override
	public ApiResult<MainIndex> selectMainIndexById(Integer id) {
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, mainIndexMapper.selectByPrimaryKey(id));
	}

	@Override
	public ApiResult<List<MainIndex>> getFrontMainIndex(Integer type, Integer count) {
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, mainIndexMapper.getFrontMainIndex(type, count));
	}
}
